﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace TCPCommClient
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient tcpc = new TcpClient();
            Byte[] read = new Byte[32];
            String server = "127.0.0.1";
            // 验证服务器是否存在
            if (Dns.GetHostByName(server) == null)
            {
                Console.WriteLine("找不到服务器：+ 服务器");
                Console.ReadKey();
                return;
            }
            // 尝试连接到服务器
            try
            {
                tcpc.Connect(server, 13);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
                return;
            }

            if (!tcpc.Connected)
            {
                Console.WriteLine("无法连接到服务器： + 服务器");
                Console.ReadKey();
                return;
            }
            // 获取流
            Stream s = tcpc.GetStream();
            // 读取流并将它转换为 ASCII 码形式
            int bytes = s.Read(read, 0, read.Length);
            String Time = Encoding.ASCII.GetString(read);
            // 显示数据
            Console.WriteLine("已接收到的字节" + bytes);
            Console.WriteLine("当前日期和时间是：" + Time);
            tcpc.Close();
            Console.ReadKey();
        }
    }
}
